package com.mq;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Procer2_10 {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("producer_group",true);
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.start();

        List<Order> orders1 = buileOrderList(1, "A", "b", "C");
        List<Order> orders2 = buileOrderList(2, "A", "b");
        List<Order> orders3 = buileOrderList(3, "A", "C");

        List<Order> orders4 =new ArrayList<>();
        orders4.addAll(orders1);
        orders4.addAll(orders2);
        orders4.addAll(orders3);

        List<Message> messages = new ArrayList<>();
        for (int i = 0; i < orders4.size(); i++) {
            Message msg = new Message(
                    "batch_topic",
                    "batch_tag",
                    ("( " + i + " ) "+orders4.get(i).toString()+"").getBytes());
            msg.setKeys("batch_key");
            messages.add(msg);
        }
        System.out.println("===>"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
        producer.send(messages);
        System.out.println("Send Finished.===>"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
    }

    public static List<Order> buileOrderList(int id,String... strings){
        List<Order> arrayList = new ArrayList<>();
        for (String string : strings) {
            Order order = new Order();
            order.orderId=id;
            order.desc=string;
            arrayList.add(order);
        }
        return arrayList;
    }


    public static class Order{
        int orderId;
        String desc;

        @Override
        public String toString() {
            return "Order{" +
                    "orderId=" + orderId +
                    ", desc='" + desc + '\'' +
                    '}';
        }
    }
}
